package com.zhn;

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.Size;
import org.opencv.videoio.VideoCapture;
import org.opencv.imgproc.Imgproc;

public class ShaoShuai {
    static {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    }

    private static final String ASCII_CHARS = "@%#*+=-:. ";

    public static void main(String[] args) {
        VideoCapture capture = new VideoCapture("D:\\java_project\\test\\src\\main\\java\\com\\zhn\\img\\ss.mp4"); // 替换为你的视频路径
        Mat frame = new Mat();

        if (!capture.isOpened()) {
            System.out.println("Error: Could not open video.");
            return;
        }

        while (true) {
            if (!capture.read(frame)) {
                break;
            }

            // 缩放帧
            Mat resizedFrame = new Mat();
            Imgproc.resize(frame, resizedFrame, new Size(120, 40)); // 调整尺寸

            StringBuilder asciiFrame = new StringBuilder();
            Mat grayFrame = new Mat();
            Imgproc.cvtColor(resizedFrame, grayFrame, Imgproc.COLOR_BGR2GRAY);

            for (int y = 0; y < grayFrame.rows(); y++) { // 不再每隔一行
                for (int x = 0; x < grayFrame.cols(); x++) { // 不再每隔一列
                    double pixel = grayFrame.get(y, x)[0];
                    asciiFrame.append(pixelToAscii((int) pixel));
                }
                asciiFrame.append("\n");
            }

            // 清屏
            System.out.print("\033[H\033[2J");
            System.out.flush();

            System.out.print(asciiFrame.toString());
            try {
                Thread.sleep(10); // 控制播放速度
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        capture.release();
    }

    private static char pixelToAscii(int pixel) {
        return ASCII_CHARS.charAt(pixel / 32); // 映射到ASCII字符
    }
}
